/**
 * 课程管理模块
 * 开发者：dev_B
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../Include/course_pub.h"
#include "../Include/student_pub.h"

// 模块内部数据结构
Course courses[MAX_COURSES];
int course_count = 0;

// 添加外部声明
extern Student students[MAX_STUDENTS];
extern int student_count;

void init_course_system() {
    course_count = 0;
    printf("课程管理系统初始化完成\n");
}

int add_course(const char* id, const char* name, const char* type, int hours, int credits, int max_students) {
    // 检查课程数量是否已达上限
    if(course_count >= MAX_COURSES) {
        printf("错误：课程数量已达上限(%d)\n", MAX_COURSES);
        return -1;
    }

    // 检查课程编号是否已存在
    if(find_course_by_id(id) != NULL) {
        printf("错误：课程编号 %s 已存在\n", id);
        return -1;
    }

    // 检查参数有效性
    if(strlen(id) == 0 || strlen(name) == 0 || strlen(type) == 0) {
        printf("错误：课程编号、名称和性质不能为空\n");
        return -1;
    }

    if(hours <= 0 || credits <= 0 || max_students <= 0) {
        printf("错误：学时、学分和人数上限必须大于0\n");
        return -1;
    }

    // 添加到数组
    Course* new_course = &courses[course_count];
    strncpy(new_course->id, id, MAX_ID_LEN - 1);
    strncpy(new_course->name, name, MAX_NAME_LEN - 1);
    strncpy(new_course->type, type, MAX_TYPE_LEN - 1);
    new_course->hours = hours;
    new_course->credits = credits;
    new_course->selected_count = 0;
    new_course->max_students = max_students;

    course_count++;
    printf("成功添加课程：%s %s (%s)\n", id, name, type);
    return 0;
}

int delete_course(const char* id) {
    for(int i = 0; i < course_count; i++) {
        if(strcmp(courses[i].id, id) == 0){
            // 检查是否有学生选了这门课
            if(courses[i].selected_count > 0) {
                printf("错误：课程 %s 还有 %d 名学生选课，无法删除\n", id, courses[i].selected_count);
                return -1;
            }

            printf("删除课程：%s %s\n", courses[i].id, courses[i].name);

            // 将最后一个元素移到当前位置
            if(i < course_count - 1) {
                courses[i] = courses[course_count - 1];
            }
            course_count--;
            return 0;
        }
    }
    printf("错误：未找到课程编号 %s 的课程\n", id);
    return -1;
}

int modify_course_info(const char* id, const char* new_name, const char* new_type,
                int new_hours, int new_credits, int new_max) {
    Course* course = find_course_by_id(id);
    if(course != NULL) {
        // 检查新的人数上限是否小于当前已选人数
        if(new_max < course->selected_count) {
            printf("错误：新的人数上限(%d)小于已选人数(%d)\n", new_max, course->selected_count);
            return -1;
        }

        printf("修改课程信息：%s %s -> %s\n", course->id, course->name, new_name);
        strncpy(course->name, new_name, MAX_NAME_LEN - 1);
        strncpy(course->type, new_type, MAX_TYPE_LEN - 1);
        course->hours = new_hours;
        course->credits = new_credits;
        course->max_students = new_max;
        return 0;
    }
    printf("错误：未找到课程编号 %s 的课程\n", id);
    return -1;
}

Course* find_course_by_id(const char* id) {
    for(int i = 0; i < course_count; i++) {
        if(strcmp(courses[i].id, id) == 0) {
            return &courses[i];
        }
    }
    return NULL;
}

void list_all_courses() {
    printf("\n=== 所有课程信息 ===\n");
    if(course_count == 0) {
        printf("暂无课程信息\n");
        return;
    }

    printf("总数：%d\n", course_count);
    for(int i = 0; i < course_count; i++) {
        printf("[%d] 编号：%s, 名称：%s, 性质：%s\n", i + 1, courses[i].id, courses[i].name, courses[i].type);
        printf("     学时：%d, 学分：%d, 已选：%d/%d\n", courses[i].hours, courses[i].credits, courses[i].selected_count, courses[i].max_students);
    }
}

void show_course_details(const char* course_id) {
    Course* course = find_course_by_id(course_id);
    if(course == NULL) {
        printf("错误：未找到课程编号 %s 的课程\n", course_id);
        return;
    }

    printf("\n=== 课程详细信息 ===\n");
    printf("课程编号：%s\n", course->id);
    printf("课程名称：%s\n", course->name);
    printf("课程性质：%s\n", course->type);
    printf("授课学时：%d\n", course->hours);
    printf("学分：%d\n", course->credits);
    printf("选课情况：%d/%d\n", course->selected_count, course->max_students);

    if(course->selected_count > 0) {
        printf("选课学生列表：\n");
        

	//遍历所有学生，查找选了这门课的学生
	int found_count = 0;
	for(int i = 0; i < student_count; i++) {
		for(int j = 0; j < students[i].course_count; j++) {
			if(strcmp(students[i].selected_courses[j], course_id) == 0) {
				printf(" %s - %s\n",students[i].id, students[i].name);
				found_count++;
				break;//找到后跳出内层循环
			}
		}
	}

	if(found_count == 0) {
		printf(" 选课关系不完整\n");
	}
    } else {
        printf("暂无学生选课\n");
    }
}

int get_course_count() {
    return course_count;
}

int add_student_to_course(const char* course_id, const char* student_id) {
    Course* course = find_course_by_id(course_id);
    if(course == NULL) {
        printf("错误：未找到课程 %s\n", course_id);
        return -1;
    }

    // 检查课程是否已满
    if(course->selected_count >= course->max_students) {
        printf("错误：课程 %s 人数已满(%d/%d)\n", course_id, course->selected_count, course->max_students);
        return -1;
    }

    // 增加选课人数
    course->selected_count++;
    printf("课程 %s 增加选课学生: %s (当前: %d/%d)\n", course_id, student_id, course->selected_count, course->max_students);
    return 0;
}

int remove_student_from_course(const char* course_id, const char* student_id) {
    Course* course = find_course_by_id(course_id);
    if(course == NULL) {
        printf("错误：未找到课程 %s\n", course_id);
        return -1;
    }

    // 检查是否有学生选课
    if(course->selected_count <= 0) {
        printf("错误：课程 %s 没有学生选课\n", course_id);
        return -1;
    }

    // 减少选课人数
    course->selected_count--;
    printf("课程 %s 移除选课学生: %s (当前: %d/%d)\n", course_id, student_id, course->selected_count, course->max_students);
    return 0;
}
